package com.foogui.primary.lock;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo implements Runnable{
    public static ReentrantLock lock =new ReentrantLock();

    private static int sum;
    @Override
    public void run() {

        for (int i = 0; i < 100; i++) {
            lock.lock();

            try {
                System.out.println(Thread.currentThread().getId()+"正在运行,sum="+sum);
                sum++;
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLockDemo demo = new ReentrantLockDemo();
        Thread t1 = new Thread(demo);
        Thread t2 = new Thread(demo);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("结果为"+sum);
    }
}
